package com.fz.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fz.admin.mapper.CustomerMapper;
import com.fz.admin.mapper.GoodsMapper;
import com.fz.admin.mapper.ProOrderMapper;
import com.fz.mobile.mapper.MShoppingAddressMapper;
import com.fz.model.Address;
import com.fz.model.Customer;
import com.fz.model.Goods;
import com.fz.model.Order;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/7/17.
 */
@Controller
public class ShopOrder {
    @Autowired
    private ProOrderMapper order;
    @Autowired
    private GoodsMapper goods;
    @Autowired
    private MShoppingAddressMapper adds;

    @Autowired
    private CustomerMapper cust;
     /*查询*/
    @RequestMapping(value = "/fz_order_select.do",produces = {"application/text;charset=UTF-8"}) @ResponseBody
    public String order(){
        List<Map<String,Object>> list = order.queryAll02();

        for(Map<String,Object> go: list){
            int id =Integer.parseInt(String.valueOf(go.get("cust_id")));
           // int  addid =Integer.parseInt(String.valueOf(go.get("add_id")));
            Customer customer =cust.queryById(id);

            String name =customer.getName();
            go.put("custId",name);
             List<Address> add = adds.query(id);
              String add02 =  add.get(0).getAddress();
              go.put("add_id",add02);


            int state =Integer.parseInt(String.valueOf(go.get("state")));
            if(state ==0){
                go.put("state","待付款");
            }else  if(state ==1){
                go.put("state","已支付");
            }else  if(state== 2){
                go.put("state","已取消");
            }else  if(state==3){
                go.put("state","已完成");
            }
            int pay_status =Integer.parseInt(String.valueOf(go.get("pay_status")));
            if(pay_status ==0){
                go.put("pay_status","待付款");
            }else  if(pay_status ==1){
                go.put("pay_status","已支付");
            }
            int fhstatus =Integer.parseInt(String.valueOf(go.get("fhstatus")));
            if(fhstatus ==0){
                go.put("fhstatus","待发货");
            }else  if(fhstatus ==1){
                go.put("fhstatus","已发货");
            }else  if(fhstatus== 2){
                go.put("fhstatus","退货中");
            }else  if(fhstatus==3){
                go.put("fhstatus","已退货");
            }
           String times =go.get("time")+"";
            go.put("time",times);
        }
       // System.out.println(list+"2222");
        return JSON.toJSONString(list);
    }
      /*修改返回值*/
    @RequestMapping(value = "/fz_order_insert01.do", produces = {"application/text;charset=UTF-8"}) @ResponseBody
    public String  insert01(HttpServletRequest req){
        String idd =req.getParameter("id");
        int id  =Integer.parseInt(idd);
        Order ord = order.selectById(id);
        return JSON.toJSONString(ord);
    }
     /*进数据库修改*/
    @RequestMapping("/fz_order_insert.do")  @ResponseBody
    public void insert(HttpServletRequest req){

        String ss = req.getParameter("data");
        System.out.println(ss);
        Order ord = new Order();

         JSONObject js = JSONObject.parseObject(ss);
        int id =js.getInteger("id");
        //int custId =js.getInteger("custId");
        int addId =js.getInteger("addId");
        //String orderNum =js.getString("orderNum");
        Double total = js.getDouble("total");
        int state = js.getInteger("state");

        int payState =js.getInteger("payStatus");
        int payType =js.getInteger("payType");
        String serialNo =js.getString("serialNo");
        int fhStatus =js.getInteger("fhStatus");

        ord.setId(id);

       // ord.setCustId(custId);
        ord.setAddId(addId);
       // ord.setOrderNum(orderNum);
        ord.setTotal(total);
        ord.setState(state);

        ord.setPayStatus(payState);
        ord.setPayType(payType);
        ord.setFhStatus(fhStatus);
        ord.setSerialNo(serialNo);
        int nn= order.updateById(ord);
        System.out.println(nn);
    }

    /* 删除数据*/
    @RequestMapping("/ad_order_delete.do")  @ResponseBody
    public void delete(HttpServletRequest req) {
        String idd =req.getParameter("id");
        int id =Integer.parseInt(idd);

        order.deleteById(id);
    }
}
